﻿using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1._Controls
{
	[ValidationProperty("Value")]
	public partial class DicTextBoxSelector : Hxsoft.AppV1.UserControl
	{
		protected string DataValueField
		{
			get { return TextType.HasValue ? TextType.Value == DicTextType.Value ? "Value" : "Name" : "Name"; }
		}

		public bool Enabled
		{
			get { return CtrSelectorValue.Enabled; }
			set { CtrSelectorValue.Enabled = value; }
		}

		private LoadedValue<int?> loadedSiteId;
		public int? SiteId
		{
			get
			{
				if (loadedSiteId.Loader == null)
					loadedSiteId.Loader = () => (int?)ViewState["SiteId"];
				return loadedSiteId.Value;
			}
			set
			{
				if (loadedSiteId.Setter == null)
					loadedSiteId.Setter = v =>
					{
						loadedClassId.ResetValue();
						ViewState["SiteId"] = v;
					};
				loadedSiteId.Value = value;
			}
		}

		private LoadedValue<string> loadedClassCode;
		public string ClassCode
		{
			get
			{
				if (loadedClassCode.Loader == null)
					loadedClassCode.Loader = () => (string)ViewState["ClassCode"];
				return loadedClassCode.Value;
			}
			set
			{
				if (loadedClassCode.Setter == null)
					loadedClassCode.Setter = v =>
					{
						loadedClassId.ResetValue();
						ViewState["ClassCode"] = v;
					};
				loadedClassCode.Value = value;
			}
		}

		private LoadedValue<int> loadedClassId;
		protected int ClassId
		{
			get
			{
				if (loadedClassId.Loader == null)
					loadedClassId.Loader = () => { return DicClasses.GetIdentifier(SiteId, ClassCode).Value; };
				return loadedClassId.Value;
			}
		}

		private LoadedValue<DicTextType?> loadedTextType;
		public DicTextType? TextType
		{
			get
			{
				if (loadedTextType.Loader == null)
					loadedTextType.Loader = () => (DicTextType?)ViewState["TextType"];
				return loadedTextType.Value;
			}
			set
			{
				if (loadedTextType.Setter == null)
					loadedTextType.Setter = v => ViewState["TextType"] = v;
				loadedTextType.Value = value;
			}
		}

		public string Value
		{
			get { return CtrSelectorValue.Text; }
			set { CtrSelectorValue.Text = value; }
		}

		protected override void OnPreRender(EventArgs e)
		{
			this.ApplicationPage.RegisterHeadIncludeFile(ApplicationConfiguration.JQueryUrl, IncludeFileType.JS);
			this.ApplicationPage.RegisterHeadIncludeFile(ApplicationConfiguration.JQueryBgiframeUrl, IncludeFileType.JS);
			this.ApplicationPage.RegisterHeadIncludeFile(ApplicationConfiguration.CommonScriptUrl, IncludeFileType.JS);
			this.ApplicationPage.RegisterHeadIncludeFile(ApplicationConfiguration.DicTextBoxSelectorStyleUrl, IncludeFileType.CSS);

			ScriptManager.RegisterStartupScript(this, this.GetType(), this.ID + "_Init", "$(" + this.ClientID + "_Init);", true);

			base.OnPreRender(e);
		}

		protected void CtrList_DataBinding(object sender, EventArgs e)
		{
			CtrList.DataSource = Dictionaries.GetItems(ClassId, null);
		}
	}
}